const restify = require('restify')
const collect = require('./collect')
const exec = require('child_process').exec
const cnf = require('./config')
const fs = require('fs')
const _ = require('lodash/fp')
const tf = require('./tf-idf')

var server = restify.createServer();
server.use(restify.bodyParser())

//start cut word service
const cut = exec(`${cnf.cut_bin_path} --threads 1 --segmentor-model ${cnf.cut_seg_model} --segmentor-lexicon ${cnf.cut_user_lexicon}`)

server.post('/collect', (req, res, next) => {
    res.send('采集中')
    collect(req.params.url, emiter).catch(console.error)
});

server.post('/analysis', (req, res, next) => {
    const limit = req.params.limit || 20
    const text = req.params.text || ''
    cut.stdin.write(`${text.replace(/[\r\n]/g, '')}\r\n`)
    cut.stdout.once('data', data => {
        res.send(tf(data.toString('utf8').split('\t'), limit))
    })
})

server.listen(8080, '127.0.0.1', function () {
    console.log('%s listening at %s', server.name, server.url);
});

